<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>


          <a-col :span="12">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradeId">
              <j-dict-select-tag @change="changeGradeId" type="list" v-model="model.gradeId"
                                 dictCode='cou_grade,grade_name,id,tenant_id  is not null order by  grade_order'
                                 placeholder="年级" style="width: 40%"/>
              <j-dict-select-tag type="list" v-model="model.targetNewClassId"
                                 :dictCode="queryParamSelect.targetNewClassIdSelect"
                                 @change="changeTargetNewClassId"
                                 placeholder="班级" style="width: 60%"/>
            </a-form-model-item>
          </a-col>

<!--          <a-col :span="6">-->
<!--            <a-form-model-item  :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">-->
<!--              <j-dict-select-tag type="list" v-model="model.classId"-->
<!--                                 :dictCode="queryParamSelect.targetNewClassIdSelect"-->
<!--                                 placeholder="班级" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col  :span="12">
            <a-form-model-item label="届级" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="studentEnrollmentYear">
              <j-dict-select-tag label="届级" @change="changeStudentEnrollmentYear" type="list" v-model="model.studentEnrollmentYear" dictCode="" placeholder="请选择届级"
                                 dictCode="cou_gradesession_year,gradesession_year_name,gradesession_year_code,iz_open=1 and del_flag=0 order by gradesession_year_code desc" />

            </a-form-model-item>

          </a-col>
          <a-col :span="12">
            <a-form-model-item label="序号" @change="changeStudentOrder" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentOrder">
              <a-input-number v-model="model.studentOrder" placeholder="排序号决定显示顺序" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="学生学号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentNo">
              <a-input v-model="model.studentNo" placeholder="请输入学生学号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="学生姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentName">
              <a-input v-model="model.studentName" placeholder="请输入学生姓名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentIdCardNo">
              <a-input v-model="model.studentIdCardNo" placeholder="请输入身份证号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="卡号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentCardNo">
              <a-input v-model="model.studentCardNo" placeholder="请输入卡号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="学生性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentGender">
              <!--              <a-input-number v-model="model.studentGender" placeholder="请输入学生性别" style="width: 100%" />-->
              <j-dict-select-tag type="list" v-model="model.studentGender" dictCode="sex" placeholder="请选择学生性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="出生年月" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentDateBirth">
              <j-date placeholder="请选择出生年月" v-model="model.studentDateBirth" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="民族" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentNation">
              <!--              <a-input-number v-model="model.studentNation" placeholder="请输入民族" style="width: 100%" />-->
              <j-dict-select-tag type="list" v-model="model.studentNation" dictCode="common_nation"
                                 placeholder="请输入民族" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentTelephone">
              <a-input v-model="model.studentTelephone" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="入学年份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentEnrollmentYear">-->
<!--              <a-input v-model="model.StudentEnrollmentYear" placeholder="请输入入学年份"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="学生微信" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentWechat">
              <a-input v-model="model.studentWechat" placeholder="请输入学生微信"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="学生邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentEmail">
              <a-input v-model="model.studentEmail" placeholder="请输入学生邮箱"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="学生QQ" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentQq">
              <a-input v-model="model.studentQq" placeholder="请输入学生QQ"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="身高" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentHeight">
              <a-input-number v-model="model.studentHeight" placeholder="请输入身高" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="体重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentWeight">
              <a-input-number v-model="model.studentWeight" placeholder="请输入体重" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="视力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentVision">
              <a-input-number v-model="model.studentVision" placeholder="请输入视力" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="来源学校" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentFromSchool">
              <a-input v-model="model.studentFromSchool" placeholder="请输入来源学校"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="毕业去向" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="studentGraduationDestination">
              <a-input v-model="model.studentGraduationDestination" placeholder="请输入毕业去向"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="户籍地址" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="studentHouseholdRegister">
              <a-input v-model="model.studentHouseholdRegister" placeholder="请输入户籍地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生源地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentPlaceOrigin">
              <a-input v-model="model.studentPlaceOrigin" placeholder="请输入生源地"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="现住地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentCurrentAddress">
              <a-input v-model="model.studentCurrentAddress" placeholder="请输入现住地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="学生类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentTtype">
              <!--              <a-input-number v-model="model.studentTtype" placeholder="请输入学生类型" style="width: 100%" />-->
              <j-dict-select-tag type="list" v-model="model.studentTtype" dictCode="student_ttype"
                                 placeholder="请输入学生类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="家庭状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentFamilyStatus">
              <!--              <a-input-number v-model="model.studentFamilyStatus" placeholder="请输入家庭状态" style="width: 100%" />-->
              <j-dict-select-tag type="list" v-model="model.studentFamilyStatus"
                                 dictCode="student_family_status" placeholder="请选择家庭状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否贫困生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentPoor">
              <!--              <a-input-number v-model="model.studentPoor" placeholder="请输入是否贫困生" style="width: 100%" />-->
              <j-dict-select-tag type="list" v-model="model.studentPoor" dictCode="student_poor"
                                 placeholder="请选择是否贫困生" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="贫困级别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentPoorLevel">
              <!--              <a-input-number v-model="model.studentPoorLevel" placeholder="请输入贫困级别" style="width: 100%" />-->
              <j-dict-select-tag type="list" v-model="model.studentPoorLevel" dictCode="student_poor_level"
                                 placeholder="请输入贫困级别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否借读生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentLoan">
              <!--              <a-input-number v-model="model.studentLoan" placeholder="请输入是否借读生" style="width: 100%" />-->
              <j-dict-select-tag type="list" v-model="model.studentLoan" dictCode="student_load"
                                 placeholder="请选择是否借读生" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="特殊标识" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentSpecialFlag">
              <a-input-number v-model="model.studentSpecialFlag" placeholder="请输入特殊标识" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="紧急联系人" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="studentEmergencyContact">
              <a-input v-model="model.studentEmergencyContact" placeholder="请输入紧急联系人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="紧急人联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="studentEmergencyTelephone">
              <a-input v-model="model.studentEmergencyTelephone" placeholder="请输入紧急人联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="学生状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentStatus">
              <!--              <a-input-number v-model="model.studentStatus" placeholder="请选择学生状态" style="width: 100%" />-->
              <j-dict-select-tag type="list" v-model="model.studentStatus" dictCode="student_status"
                                 placeholder="请选择学生状态" />
            </a-form-model-item>
          </a-col>


          <a-col :span="12">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentIzOpen">
              <j-dict-select-tag v-model="model.studentIzOpen" dict-code="iz_open"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentRemark">
              <a-input v-model="model.studentRemark" placeholder="备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
// import { deepClone, validateDuplicateValue ,cloneObject} from '@/utils/util'
import { filterObj,cloneObject,deepClone } from '@/utils/util';
import Vue from 'vue'
import { TENANT_ID } from '@/store/mutation-types'
import { getAllClass, getClassById } from '@api/commonAPI'
import { addZero } from '../../../../components/_util/StringUtil'//@components/_util/StringUtil'

export default {
  name: 'StuStudentForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      queryParamSelect:{targetNewClassIdSelect: 'cou_class cc,cc.class_name,cc.id ,cc.class_iz_delete =0 and cc.class_iz_open  =1 order by cc.grade_code !! cc.class_order !! cc.class_number'},
      model: {
        // targetNewClassId: this.modelDefault.classId,
        studentStatus: 1,
        studentIzOpen: 1,
        studentLoan: 0,
        studentPoor: 0,
        studentFamilyStatus: 1,
        studentTtype: 1,
        studentOrder: 1,
        classNumber: null,
        studentEnrollmentYear: null

      },
      labelCol: {
        xs: { span: 12 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 12 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        studentName: [
          { required: true, message: '请输入学生姓名!' }
        ],
        studentStatus: [
          { required: true, message: '请输入学生状态(1正常，2休学，3退学，4转出，5转入，12毕业）!' }
        ],
        tenantId: [
          { required: true, message: '请输入学校编码!' }
        ]
      },
      url: {
        add: '/student/stuStudent/add',
        edit: '/student/stuStudent/edit',
        queryById: '/student/stuStudent/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    // this.model.targetNewClassId = deepClone( this.modelDefault.classId ) //深拷贝，改变不影响原来的值
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    changeGradeId() {
      if (this.model.gradeId != null && this.model.gradeId != '') {
        this.queryParamSelect.targetNewClassIdSelect = 'cou_class cc,cc.class_name,cc.id,cc.grade_code =\'' + this.model.gradeId + '\' and cc.class_iz_delete =0 and cc.class_iz_open  =1 order by cc.grade_code !! cc.class_order !! cc.class_number'
      }
    },
    async getClassNumber(classId) {
      let res = await getClassById(classId)

      let classGradesessionYear = res.result.classGradesessionYear
      let classNumber = res.result.classNumber
      this.model.classNumber = cloneObject(classNumber)
      this.model.studentEnrollmentYear = cloneObject(classGradesessionYear)
      return classNumber
    },
    async getclassGradesessionYear(classId) {
      let res = await getClassById(classId)
      let classGradesessionYear = res.result.classGradesessionYear
      let classNumber = res.result.classNumber
      this.model.classNumber = cloneObject(classNumber)
      this.model.studentEnrollmentYear = cloneObject(classGradesessionYear)
      return classGradesessionYear
    },
    //创建学生学号
    createStudentNo(tenantid,classGradesessionYear,classNumber,studentOrder){
      this.model.studentNo = null
      let tenantid2 =  addZero(tenantid,4)
      let classGradesessionYear2 =  addZero(classGradesessionYear,4)
      let classNumber2 =  addZero(classNumber,2)
      let studentNo2 =   addZero(this.model.studentOrder,2)
      let studentNo3 = cloneObject(''+tenantid2 + classGradesessionYear2 + classNumber2  + studentNo2)
      this.model.studentNo = cloneObject(studentNo3)
      return studentNo3;
    },
    //初始化班级信息-->更改届级、学号
    async changeTargetNewClassId(){
      let tenantid = Vue.ls.get(TENANT_ID);
      // this.model.studentEnrollmentYear = null
      let classGradesessionYear = await  this.getclassGradesessionYear(this.model.targetNewClassId)
      this.model.studentEnrollmentYear = cloneObject(classGradesessionYear);

      let classNumber = await  this.getClassNumber(this.model.targetNewClassId);//通过班级id查找对应的班级号
      classNumber =  addZero(classNumber,2)

      let studentNo =   addZero(this.model.studentOrder,2)
      this.model.studentNo = this.createStudentNo(tenantid , classGradesessionYear , classNumber  , studentNo);//''+tenantid + classGradesessionYear + classNumber  + studentNo

      // if(!this.model.studentNo || this.model.studentNo.indexOf(classNumber) == -1){
      //   this.model.studentNo =  tenantid+this.model.studentEnrollmentYear+classNumber+this.model.studentOrder;
      // }
    },
     changeStudentEnrollmentYear(){
      let tenantid = Vue.ls.get(TENANT_ID);
      let classGradesessionYear = cloneObject(this.model.studentEnrollmentYear);

      let classNumber = cloneObject(this.model.classNumber);//await  this.getClassNumber(this.model.classId);//通过班级id查找对应的班级号
      classNumber =  addZero(classNumber,2)

      let studentNo =   addZero(this.model.studentOrder,2)

      this.model.studentNo =  this.createStudentNo(tenantid , classGradesessionYear , classNumber  , studentNo)
    },
     changeStudentOrder(){
      let tenantid = Vue.ls.get(TENANT_ID);

      let classGradesessionYear = this.model.studentEnrollmentYear;

      let classNumber = this.model.classNumber;//await  this.getClassNumber(this.model.classId);//通过班级id查找对应的班级号
      classNumber =  addZero(classNumber,2)

      let studentNo =   addZero(this.model.studentOrder,2)
      this.model.studentNo =  this.createStudentNo(tenantid , classGradesessionYear , classNumber  , studentNo)
    }
  }
}
</script>